package tr.com.netgis.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import tr.com.netgis.pojo.NGLocation;


/**
 * 
 * @author NetGis
 *
 */
public class StatusSpeedStrategyLastNLocations extends AStatusSpeedStrategy {


	public StatusSpeedStrategyLastNLocations() {
		super();
	}

	public StatusSpeedStrategyLastNLocations(RotatingNGLocation rLocations) {
		super(rLocations);
	}
	
	@Override
	public float calculateSpeed(NGLocation currentLocation, RotatingNGLocation rLocations) {
		List<NGLocation> locations = rLocations.toList();
		if (Utility.isEmpty(locations))
			return 0;
		//sort locations tarihSaat asc
		Collections.sort(locations);
		NGLocation loc0 = locations.get(0);
		NGLocation locLast = currentLocation;
		if (loc0.equals(locLast))
			return 0;
		//dAverage = distance( location(0) , location(n) ); // son en konumdaki toplam yer değiştirme		
		return Utility.speed(loc0, locLast);		
	}
	
	public static void main(String[] args) throws ParseException {
		NGLocation l1 = new NGLocation();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		/*
		l1.setTarihSaat(sdf.parse("2012-02-02 09:12:56").getTime());
		l1.setLat( 39.91228265);
		l1.setLon( 32.85587969);
		
		NGLocation l2 = new NGLocation();
		l2.setTarihSaat(sdf.parse("2012-02-02 09:14:05").getTime());
		l2.setLat( 39.90870255);
		l2.setLon( 32.85697024);
		
		NGLocation l3 = new NGLocation();
		l3.setTarihSaat(sdf.parse("2012-02-02 09:15:11").getTime());
		l3.setLat(39.91173954);
		l3.setLon(32.85499707);
		
		NGLocation l4 = new NGLocation();
		l4.setTarihSaat(sdf.parse("2012-02-02 09:17:17").getTime());
		l4.setLat(39.91615097);
		l4.setLon(32.83430951);

		NGLocation l5 = new NGLocation();
		l5.setTarihSaat(sdf.parse("2012-02-02 09:18:07").getTime());
		l5.setLat(39.91474187);
		l5.setLon(32.82301695);

		NGLocation l6 = new NGLocation();
		l6.setTarihSaat(sdf.parse("2012-02-02 09:19:28").getTime());
		l6.setLat(39.90925385);
		l6.setLon(32.79849436);		
		
		NGLocation l7 = new NGLocation();
		l7.setTarihSaat(sdf.parse("2012-02-02 09:21:13").getTime());
		l7.setLat(39.90818836);
		l7.setLon(32.76910355);	
		
		NGLocation l8 = new NGLocation();
		l8.setTarihSaat(sdf.parse("2012-02-02 09:22:03").getTime());
		l8.setLat(39.90605476);
		l8.setLon(32.76438507);	//--

		NGLocation l9 = new NGLocation();
		l9.setTarihSaat(sdf.parse("2012-02-02 09:24:04").getTime());
		l9.setLat(39.89952573);
		l9.setLon(32.76675108);	
		
		NGLocation l10 = new NGLocation();
		l10.setTarihSaat(sdf.parse("2012-02-02 09:26:17").getTime());
		l10.setLat(39.89699983);
		l10.setLon(32.77509373);
		
		NGLocation l11 = new NGLocation();
		l11.setTarihSaat(sdf.parse("2012-02-02 09:28:05").getTime());
		l11.setLat(39.89925163);
		l11.setLon(32.77332209);
		
		NGLocation l12 = new NGLocation();
		l12.setTarihSaat(sdf.parse("2012-02-02 09:29:00").getTime());
		l12.setLat(39.89980714);
		l12.setLon(32.77383759);

		NGLocation l13 = new NGLocation();
		l13.setTarihSaat(sdf.parse("2012-02-02 10:50:59").getTime());
		l13.setLat(39.89950396);
		l13.setLon(32.77351958);
		*/
		
		
		l1.setTarihSaat(sdf.parse("2012-02-02 23:36:39").getTime());
		l1.setLat( 39.9138029);
		l1.setLon( 32.859368);
		
		NGLocation l2 = new NGLocation();
		l2.setTarihSaat(sdf.parse("2012-02-02 23:37:24").getTime());
		l2.setLat( 39.9138029);
		l2.setLon( 32.859368);
		
		NGLocation l3 = new NGLocation();
		l3.setTarihSaat(sdf.parse("2012-02-02 23:38:14").getTime());
		l3.setLat(39.9138029);
		l3.setLon(32.859368);
		
		NGLocation l4 = new NGLocation();
		l4.setTarihSaat(sdf.parse("2012-02-02 23:39:05").getTime());
		l4.setLat(39.913717250000005);
		l4.setLon(32.8593862);

		NGLocation l5 = new NGLocation();
		l5.setTarihSaat(sdf.parse("2012-02-02 23:39:55").getTime());
		l5.setLat(39.9138029);
		l5.setLon(32.859368);

		NGLocation l6 = new NGLocation();
		l6.setTarihSaat(sdf.parse("2012-02-02 23:40:45").getTime());
		l6.setLat(39.9138029);
		l6.setLon(32.859368);	
		
		NGLocation l7 = new NGLocation();
		l7.setTarihSaat(sdf.parse("2012-02-02 23:41:35").getTime());
		l7.setLat(39.913717250000005);
		l7.setLon(32.8593862);	
		
		
		l1.setSent((byte)1);
		RotatingNGLocation lastNLocationList = new RotatingNGLocation(10);
		lastNLocationList.insertElement(l1);l1.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l2);l2.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l3);l3.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l4);l4.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l5);l5.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l6);l6.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l7);l7.calculateSpeedAndStatus(lastNLocationList);
		
		/*
		lastNLocationList.insertElement(l8);l8.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l9);l9.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l10);l10.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l11);l11.calculateSpeedAndStatus(lastNLocationList);
		lastNLocationList.insertElement(l12);l12.calculateSpeedAndStatus(lastNLocationList);	
		lastNLocationList.insertElement(l13);l13.calculateSpeedAndStatus(lastNLocationList);
		*/
		for (NGLocation ngLocation : lastNLocationList.toList()) {
			System.out.println(ngLocation + "\n---\n");
		}
		
	}

}
